﻿namespace WMS6.Web.Configuration
{
    using System;
    using System.Collections.Generic;
    using System.Web.UI;
    using System.Web.UI.HtmlControls;
    using System.Web.UI.WebControls;
    using WMS6.Web;
    using WMS6.Web.BaseInfoServiceRef;

    public class CycleCountClass : BasePage
    {
        protected Button btnCopy;
        protected Button btnDelete;
        protected Button btnGoBack;
        protected Button btnNew;
        protected Button btnSave;
        protected CheckBox chkIsActive;
        protected HtmlForm F2281;
        protected HtmlInputHidden hdnClassID;
        protected HtmlInputHidden hdnFlag;
        protected HtmlInputHidden hdnFrom;
        protected Label lblClassID;
        protected Label lblCycleDay;
        protected Label lblDescr;
        protected Label lblDispatchRule;
        protected Label lblMsg;
        protected Label lblNegCntVariance;
        protected Label lblNegValueVariance;
        protected Label lblPosQtyVariance;
        protected Label lblPosValueVariance;
        protected HtmlLink mycss;
        protected Label one1;
        protected ScriptManager ScriptManager1;
        protected TextBox txtClassID;
        protected TextBox txtCycleDay;
        protected TextBox txtDescr;
        protected TextBox txtNegCntVariance;
        protected TextBox txtNegValueVariance;
        protected TextBox txtPosQtyVariance;
        protected TextBox txtPosValueVariance;
        protected UpdatePanel up1;

        protected void btnCopy_Click(object sender, EventArgs e)
        {
            this.txtClassID.Text = "";
            this.txtClassID.Enabled = true;
            this.hdnClassID.Value = "";
            this.hdnFlag.Value = "0";
        }

        protected void btnDelete_Click(object sender, EventArgs e)
        {
            if (!string.IsNullOrEmpty(this.txtClassID.Text.Trim()))
            {
                try
                {
                    List<string> classIDs = new List<string> {
                        this.txtClassID.Text.Trim()
                    };
                    this.lblMsg.Text = BaseInfoServiceClientProxy.GetInstance().Delete(classIDs, "CYCLE_COUNT_CLASS") + base.GetGlobalMsgResource("C006");
                    this.btnSave.Enabled = false;
                    this.btnDelete.Enabled = false;
                    this.btnCopy.Enabled = false;
                    base.DataEntityViewState = null;
                }
                catch (Exception ex)
                {
                    this.lblMsg.Text = base.GetGlobalMsgResource("C007") + ex.Message;
                }
            }
        }

        protected void btnGoBack_Click(object sender, EventArgs e)
        {
            switch (this.hdnFrom.Value)
            {
                case "query":
                    base.Response.Redirect("CycleCountClassQuery.aspx", false);
                    break;

                case "list":
                    base.Response.Redirect("CycleCountClassList.aspx?from=edit", false);
                    break;
            }
        }

        protected void btnSave_Click(object sender, EventArgs e)
        {
            if (string.IsNullOrEmpty(this.txtClassID.Text.Trim()))
            {
                this.lblMsg.Text = base.GetGlobalMsgResource("F2620_001");
                this.txtClassID.Focus();
            }
            else if (this.hdnFlag.Value == "1")
            {
                this.UpdateCycleCountClassInfo();
            }
            else
            {
                this.InsertCycleCountClassInfo();
            }
        }

        private void InsertCycleCountClassInfo()
        {
            try
            {
                CycleCountClassInfo cycleCountClass = new CycleCountClassInfo {
                    ClassID = this.txtClassID.Text.Trim(),
                    ClassDesc = this.txtDescr.Text
                };
                if (!string.IsNullOrEmpty(this.txtCycleDay.Text.Trim()))
                {
                    cycleCountClass.CycleDays = int.Parse(this.txtCycleDay.Text.Trim());
                }
                if (!string.IsNullOrEmpty(this.txtPosQtyVariance.Text.Trim()))
                {
                    cycleCountClass.PosQtyVariance = decimal.Parse(this.txtPosQtyVariance.Text.Trim());
                }
                if (!string.IsNullOrEmpty(this.txtNegCntVariance.Text.Trim()))
                {
                    cycleCountClass.NegCntVariance = decimal.Parse(this.txtNegCntVariance.Text.Trim());
                }
                if (!string.IsNullOrEmpty(this.txtNegCntVariance.Text.Trim()))
                {
                    cycleCountClass.PosValueVariance = decimal.Parse(this.txtNegCntVariance.Text.Trim());
                }
                if (!string.IsNullOrEmpty(this.txtNegValueVariance.Text.Trim()))
                {
                    cycleCountClass.NegValueVariance = decimal.Parse(this.txtNegValueVariance.Text.Trim());
                }
                cycleCountClass.IsActive = this.chkIsActive.Checked ? "Y" : "N";
                cycleCountClass.CreatedBy = base.UserInfo.CreatedBy;
                this.hdnClassID.Value = BaseInfoServiceClientProxy.GetInstance().Insert(cycleCountClass, "CYCLE_COUNT_CLASS");
                this.lblMsg.Text = this.hdnClassID.Value + base.GetGlobalMsgResource("C004");
                base.DataEntityViewState = cycleCountClass;
                this.hdnFlag.Value = "1";
                this.txtClassID.Enabled = false;
            }
            catch (Exception ex)
            {
                this.lblMsg.Text = base.GetGlobalMsgResource("C005") + ex.Message;
            }
        }

        private void LoadData()
        {
            if (!string.IsNullOrEmpty(base.Request.QueryString["ID"]))
            {
                this.hdnClassID.Value = base.Request.QueryString["ID"].ToString();
                this.SetCycleCountClass2Form();
                this.hdnFlag.Value = "1";
            }
            if (!string.IsNullOrEmpty(base.Request.QueryString["from"]))
            {
                this.hdnFrom.Value = base.Request.QueryString["from"].ToString();
            }
        }

        protected void Page_Load(object sender, EventArgs e)
        {
            if (!base.IsPostBack)
            {
                base.InitPage(this.Page);
                this.LoadData();
            }
        }

        private void SetCycleCountClass2Form()
        {
            try
            {
                string ClassID = this.hdnClassID.Value;
                CycleCountClassInfo cycleCountClass = BaseInfoServiceClientProxy.GetInstance().GetInfoByID(ClassID, "CYCLE_COUNT_CLASS") as CycleCountClassInfo;
                base.DataEntityViewState = cycleCountClass;
                if (cycleCountClass != null)
                {
                    this.txtClassID.Text = cycleCountClass.ClassID;
                    this.txtCycleDay.Text = cycleCountClass.CycleDays.ToString();
                    this.txtDescr.Text = cycleCountClass.ClassDesc;
                    this.txtNegCntVariance.Text = cycleCountClass.NegCntVariance.ToString();
                    this.txtNegValueVariance.Text = cycleCountClass.NegValueVariance.ToString();
                    this.txtPosQtyVariance.Text = cycleCountClass.PosQtyVariance.ToString();
                    this.txtPosValueVariance.Text = cycleCountClass.PosValueVariance.ToString();
                    this.chkIsActive.Checked = cycleCountClass.IsActive == "Y";
                    this.txtClassID.Enabled = false;
                }
            }
            catch (Exception ex)
            {
                this.lblMsg.Text = ex.Message;
            }
        }

        private void UpdateCycleCountClassInfo()
        {
            try
            {
                CycleCountClassInfo cycleCountClass = base.DataEntityViewState as CycleCountClassInfo;
                cycleCountClass.ClassDesc = this.txtDescr.Text;
                cycleCountClass.CycleDays = int.Parse(this.txtCycleDay.Text.Trim());
                cycleCountClass.PosQtyVariance = decimal.Parse(this.txtPosQtyVariance.Text.Trim());
                cycleCountClass.NegCntVariance = decimal.Parse(this.txtNegCntVariance.Text.Trim());
                cycleCountClass.PosValueVariance = decimal.Parse(this.txtPosValueVariance.Text.Trim());
                cycleCountClass.NegValueVariance = decimal.Parse(this.txtNegValueVariance.Text.Trim());
                cycleCountClass.IsActive = this.chkIsActive.Checked ? "Y" : "N";
                cycleCountClass.UpdatedBy = base.UserInfo.CreatedBy;
                this.lblMsg.Text = BaseInfoServiceClientProxy.GetInstance().Update(cycleCountClass, "CYCLE_COUNT_CLASS") + base.GetGlobalMsgResource("C002");
            }
            catch (Exception ex)
            {
                this.lblMsg.Text = base.GetGlobalMsgResource("C003") + ex.Message;
            }
        }
    }
}

